import React, {PropsWithChildren, useState} from "react"
import {Popover, PopoverProps, Space} from "antd";
import {PopoverConfirmButtons} from "./PopoverConfirmButtons";

export interface ConfirmPopoverProps extends PopoverProps {
    onConfirm: ()=>Promise<void>
    description?:string
}

const ConfirmPopover: React.FC<PropsWithChildren<ConfirmPopoverProps>> = (props) => {
    const [loading,setLoading] = useState(false)
    return <>
        <Popover trigger={"click"}
                 open={props.open}
                 onOpenChange={props.onOpenChange} content={<Space direction={"vertical"}>
            <p>{props.description}</p>
            <PopoverConfirmButtons onCancel={() => {
                props.onOpenChange && props.onOpenChange(false)
            }} onConfirm={async () => {
                setLoading(true)
                try {
                    await props.onConfirm()
                } finally {
                }
                setLoading(false)
                props.onOpenChange && props.onOpenChange(false)
            }}/>
        </Space>}>
            {props.children}
        </Popover>
    </>
}

export default ConfirmPopover